<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :node-data="company" root @add-depts="addDeptsFn" />
        <!-- 树形控件 -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <template #default="{ data }">
            <TreeTools
              :node-data="data"
              @del-depts="getDepartments"
              @add-depts="addDeptsFn"
              @edit-depts="editDeptsFn"
            />
          </template>
        </el-tree>
      </el-card>
      <AddDepts
        ref="addDept"
        :dept-list="deptList"
        :node-data="nodeData"
        :show-dialog="isShow"
        @closedialog="closedialogFn"
        @add-depts="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import { reqGetDepartments } from '@/api/departments'
import TreeTools from './components/TreeTools.vue'
import AddDepts from './components/AddDepts.vue'
import { transListToTree } from '@/utils'

export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDepts
  },
  data() {
    return {
      departs: [],
      deptList: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: { name: '', manager: '负责人' },
      isShow: false,
      nodeData: {},
      loading: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 查询企业的部门列表
    async getDepartments() {
      this.loading = true

      // 需求：将来我们想着等两秒以后再去，发请求获取部门列表
      await new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve()
        }, 2000)
      })

      const {
        data: { depts, companyName }
      } = await reqGetDepartments()
      this.deptList = depts
      this.company.name = companyName
      this.company.id = ''
      const result = transListToTree(depts, '')
      this.departs = result
      this.loading = false
    },
    closedialogFn(flag) {
      this.isShow = flag
    },
    addDeptsFn(nodeData) {
      this.isShow = true
      this.nodeData = nodeData
      console.log(nodeData)
      // this.$refs.addDept.getSimpleUserList()
      // this.$nextTick(() => {
      //   this.$refs.addDept.dialogTitle = '添加子部门'
      // })
    },
    editDeptsFn(nodeData) {
      this.isShow = true
      this.nodeData = nodeData
      console.log(nodeData)
      this.$nextTick(() => {
      // this.$refs.addDept.dialogTitle = '编辑部门'
        this.$refs.addDept.getDepartDetail()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px 30px;
  font-size: 14px;
  font-weight: bolder;

  .el-tree {
    ::v-deep {
      // 小三角的样式, 去掉默认的小三角的旋转效果
      .el-tree-node__expand-icon.expanded {
        -webkit-transform: rotate(0deg);
        transform: rotate(0deg);
      }
      // 有子节点 且未展开 +
      .el-icon-caret-right:before {
        background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
        content: "";
        display: block;
        width: 16px;
        height: 16px;
        font-size: 16px;
        background-size: 16px;
      }
      // 有子节点 且已展开 -
      .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
        background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
        content: "";
        display: block;
        width: 16px;
        height: 16px;
        font-size: 16px;
        background-size: 16px;
      }
      // 没有子节点
      .el-tree-node__expand-icon.is-leaf::before {
        background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
        content: "";
        display: block;
        width: 16px;
        height: 16px;
        font-size: 16px;
        background-size: 16px;
      }
    }
  }
}
</style>
